//
//  KVOViewController.m
//  KVO和链式编程
//
//  Created by dihuijun on 17/4/13.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "KVOViewController.h"
#import "person.h"


@interface KVOViewController ()
/**   person   */
@property (nonatomic, strong) person *p;
@end

@implementation KVOViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"KVO,(运行看打印)";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    //初始化
    self.p = [person sharedPerson];
    //name ：是person的属性,添加一个kvo监听改变
    [self.p addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];

}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    NSLog(@"%@",self.p.name);
}
#pragma mark --- 只要点击就改变name的值，就会来到上面的方法 observeValueForKeyPath
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    static int i = 0;
    i++;
    self.p.name = [NSString stringWithFormat:@"name的值由于被点击而一直在改变：%zd",i];
}


@end
